package com.example.yuejian.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yuejian.R;
import com.example.yuejian.fragment.ClubFragment;
import com.example.yuejian.fragment.PostFragment;
import com.example.yuejian.fragment.fragment_me;
import com.example.yuejian.fragment.fragment_one;
import com.example.yuejian.fragment.fragment_three;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {
    private LinearLayout tab1,tab2,tab3,tab4,tab5;

    private ViewPager main_viewPager ;
    private FragmentPagerAdapter mAdapter;
    private ArrayList<Fragment> fragmentList ;

    private TextView tv1,tv2,tv3,tv4,tv5;
    private ImageButton img1,img2,img3,img4,img5;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initViews();//初始化控件
        initEvents();//初始化事件
        initDatas();//初始化数据
    }
    private void initDatas() {

        fragmentList = new ArrayList<Fragment>() ;
        //将四个Fragment加入集合中
        fragmentList.add(new fragment_one());
        fragmentList.add(new ClubFragment());
        fragmentList.add(new fragment_three());
        fragmentList.add(new PostFragment());
        fragmentList.add(new fragment_me());


        //初始化适配器
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return fragmentList.size();
            }

            @Override
            public Fragment getItem(int position) {
                // TODO Auto-generated method stub
                return fragmentList.get(position);
            }
        };

        //不要忘记设置ViewPager的适配器
        main_viewPager.setAdapter(mAdapter);
        //设置ViewPager的切换监听
        main_viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // TODO Auto-generated method stub
                main_viewPager.setCurrentItem(position);
                setImgAndText();
                selectTab(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

    }

    private void initEvents() {
        //设置四个Tab的点击事件
        tab1.setOnClickListener(onClickListener);
        tab2.setOnClickListener(onClickListener);
        tab3.setOnClickListener(onClickListener);
        tab4.setOnClickListener(onClickListener);
        tab5.setOnClickListener(onClickListener);

    }
    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //先将四个ImageButton置为灰色
            setImgAndText();

            //根据点击的Tab切换不同的页面及设置对应的ImageButton为绿色
            switch (v.getId()) {
                case R.id.tab1:
                    selectTab(0);
                    break;
                case R.id.tab2:
                    selectTab(1);
                    break;
                case R.id.tab3:
                    selectTab(2);
                    break;
                case R.id.tab4:
                    selectTab(3);
                    break;
                case R.id.tab5:
                    selectTab(4);
                    break;

            }
        }
    };

    //初始化控件
    private void initViews() {
        main_viewPager = (ViewPager) findViewById(R.id.main_ViewPager);
        img1 = (ImageButton)findViewById(R.id.img1);
        img2 = (ImageButton)findViewById(R.id.img2);
        img3 = (ImageButton)findViewById(R.id.img3);
        img4 = (ImageButton)findViewById(R.id.img4);
        img5 = (ImageButton)findViewById(R.id.img5);
        tv1 = (TextView)findViewById(R.id.tv1);
        tv2 = (TextView)findViewById(R.id.tv2);
        tv3 = (TextView)findViewById(R.id.tv3);
        tv4 = (TextView)findViewById(R.id.tv4);
        tv5 = (TextView)findViewById(R.id.tv5);
        tab1 = (LinearLayout)findViewById(R.id.tab1);
        tab2 = (LinearLayout)findViewById(R.id.tab2);
        tab3 = (LinearLayout)findViewById(R.id.tab3);
        tab4 = (LinearLayout)findViewById(R.id.tab4);
        tab5 = (LinearLayout)findViewById(R.id.tab5);

    }

    private void selectTab(int i) {
        //根据点击的Tab设置对应的ImageButton为绿色
        switch (i) {
            case 0:
                img1.setImageResource(R.drawable.peixun);
                tv1.setTextColor(this.getResources().getColor(R.color.tv_Red));
                break;
            case 1:
                img2.setImageResource(R.drawable.julebu);
                tv2.setTextColor(this.getResources().getColor(R.color.tv_Red));
                break;
            case 2:
                img3.setImageResource(R.drawable.shouye);
                tv3.setTextColor(this.getResources().getColor(R.color.tv_Red));
                break;
            case 3:
                img4.setImageResource(R.drawable.shaitu);
                tv4.setTextColor(this.getResources().getColor(R.color.tv_Red));
                break;
            case  4:
                img5.setImageResource(R.drawable.wode);
                tv5.setTextColor(this.getResources().getColor(R.color.tv_Red));
                break;

        }
        //设置当前点击的Tab所对应的页面
        main_viewPager.setCurrentItem(i);
    }

    private void setImgAndText()
    {


        img1.setImageResource(R.drawable.peixun1);
        img2.setImageResource(R.drawable.julebu1);
        img3.setImageResource(R.drawable.shouye1);
        img4.setImageResource(R.drawable.shaitu1);
        img5.setImageResource(R.drawable.wode2);






        tv1.setTextColor(this.getResources().getColor(R.color.bg_Gray));
        tv2.setTextColor(this.getResources().getColor(R.color.bg_Gray));
        tv3.setTextColor(this.getResources().getColor(R.color.bg_Gray));
        tv4.setTextColor(this.getResources().getColor(R.color.bg_Gray));
        tv5.setTextColor(this.getResources().getColor(R.color.bg_Gray));
    }

}